package common.server.controller;

import common.server.utils.Contants;
import org.slf4j.LoggerFactory;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.net.InetAddress;
import java.util.HashMap;
import java.util.Map;

/**
 * 基础的控制层信息
 * 
 * @author Administrator
 * @version [版本号, 2017/11/20 0020]
 * @see [相关类/方法]
 * @since [产品/模块版本]
 */
public class BaseController {

    private static final org.slf4j.Logger logger = LoggerFactory.getLogger(BaseController.class);
	/**
	 * 获取请求信息
	 * 
	 * @return
	 */
	protected HttpServletRequest getHttpRequest() {
		ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder
				.getRequestAttributes();
		return attributes.getRequest();
	}

	/**
	 * 获取响应信息
	 * 
	 * @return
	 */
	protected HttpServletResponse getHttpResponse() {
		ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder
				.getRequestAttributes();
		return attributes.getResponse();
	}

	/**
	 * 获取 Cookie信息
	 * 
	 * @return
	 */
	protected Cookie[] getHttpCookies() {
		return getHttpRequest().getCookies();
	}

	/**
	 * 获取Cookie
	 * 
	 * @param key
	 * @return
	 */
	protected Cookie getHttpCookie(String key) {
		Cookie cookie = null;
		for (Cookie c : getHttpCookies()) {
			if (c.getName().equals(key)) {
				cookie = c;
				break;
			}
		}
		return cookie;
	}

	/**
	 * 获取流水号
	 * 
	 * @return
	 */
	protected String getTrace() {
		return getHttpRequest().getHeader(Contants.DEFATE_TRACE_NAME);
	}

	/**
	 * 获取服务编码
	 * 
	 * @return
	 */
	protected String getServiceCode() {
		return getHttpRequest().getHeader(Contants.DEFATE_SERVICE_CODE_NAME);
	}

	/**
	 * 获取公网IP信息
	 * 
	 * @return
	 */
	protected String getChannelIp() {
        String ipAddress = getHttpRequest().getHeader("x-forwarded-for");
        if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
            ipAddress = getHttpRequest().getHeader("Proxy-Client-IP");
        }
        if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
            ipAddress = getHttpRequest().getHeader("WL-Proxy-Client-IP");
        }
        if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
            ipAddress = getHttpRequest().getRemoteAddr();
            if (ipAddress.equals("127.0.0.1") || ipAddress.equals("0:0:0:0:0:0:0:1")) {
                // 根据网卡取本机配置的IP
                InetAddress inet = null;
                try {
                    inet = InetAddress.getLocalHost();
                } catch (Exception e) {
                    logger.error("根据网卡取本机配置的IP error:{}",e);
                }
                ipAddress = inet.getHostAddress();
            }
        }
        if (ipAddress != null && ipAddress.length() > 15) {
            if (ipAddress.indexOf(",") > 0) {
                ipAddress = ipAddress.substring(0, ipAddress.indexOf(","));
            }
        }
        return ipAddress;
	}

	/**
	 * 获取FeignHeader
	 * 
	 * @return
	 */
	protected Map<String, Object> getFeignHeaderMap() {
		Map<String, Object> headerMap = new HashMap<>(2);
		headerMap.put(Contants.DEFATE_TRACE_NAME, getTrace());
		headerMap.put(Contants.DEFATE_SERVICE_CODE_NAME, getServiceCode());
		return headerMap;
	}
}
